package com.wust.tieshi;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.wust.R;
import com.wust.baby.BabyInfo;
import com.wust.baby.BabyInfoDao;
import com.wust.footprint.FirstActivity;
import com.wust.footprint.diaryList;
import com.wust.menu.FirstGroupTab;
import com.wust.menu.SecondGroupTab;
import com.wust.util.SqltimeToTime;

public class growActivity extends ListActivity {
	private ListView listv=null;
	private ImageButton imgbtn=null;
	private ArrayList<HashMap<String, Object>> listItems;
	private String baby_id;
	private BabyInfoDao infoDao;
	private List<BabyInfo>infolist;
	private String baby_date;
	private Date birth;
	private SqltimeToTime timeTransfer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.growlist);
		imgbtn=(ImageButton)findViewById(R.id.img_add_record);
		SharedPreferences preferences = getSharedPreferences("userinfo",0);
		baby_id=preferences.getString("baby_id", "");
		baby_date=preferences.getString("baby_date","");
		timeTransfer=new SqltimeToTime();
		birth=timeTransfer.stringToDate(baby_date, "yyyy-MM-dd");
		infoDao=new BabyInfoDao(this);
		
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		listv=getListView();
		listv.setAdapter(buildAdapter());
		imgbtn.setOnClickListener(new ImgListener());
		super.onResume();
	}
	
	
	private  SimpleAdapter buildAdapter(){
		infolist=infoDao.getBabyInfoListById(baby_id);
		
		listItems = new ArrayList<HashMap<String, Object>>();   
		if(infolist!=null)
		{
		for (Iterator info = infolist.iterator(); info.hasNext();) {
			BabyInfo baby = (BabyInfo) info.next();
			Date date=timeTransfer.stringToDate(baby.getInfo_date(),"yyyy-MM-dd");
			
			long age=(date.getTime()-birth.getTime())/60/60/24/30/1000;
			System.out.println("age:"+age);
			HashMap<String, Object> map1 = new HashMap<String, Object>(); 
			 map1.put("age", age);//图片   
		        map1.put("height",baby.getInfo_height()+"cm");     //文字   
		        map1.put("weight", baby.getInfo_weigth()+"kg");
		        map1.put("chest", baby.getInfo_bust()+"cm");
		        map1.put("headwidth", baby.getInfo_circumference()+"cm");
		        map1.put("beiwang", baby.getDescription());
		        listItems.add(map1); 
		}
		}         
       
        
           
        //生成适配器的Item和动态数组对应的元素       
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItems,//数据源        
            R.layout.charmlist_item,//ListItem的XML布局实现       
            //动态数组与ImageItem对应的子项               
            new String[] {"age", "height","weight","chest","headwidth","beiwang"},        
            //ImageItem的XML文件里面的一个ImageView,两个TextView ID       
            new int[] {R.id.age, R.id.height,R.id.weight,R.id.chest,R.id.headwidth,R.id.beiwang}       
        ); 
        return listItemAdapter;
	}
	@Override  
    public void onBackPressed() {   
        //把后退事件交给子Activity处理  
		Intent intent = new Intent(growActivity.this, ViewTestActivity.class);  
        // 把Activity转换成一个Window，然后转换成View  
        Window w = SecondGroupTab.group.getLocalActivityManager()  
                .startActivity("FirstActivity",intent);  
        View view = w.getDecorView();  
        //设置要跳转的Activity显示为本ActivityGroup的内容  
       SecondGroupTab.group.setContentView(view);  
    } 
	private void buildList(){
		listv.setAdapter(buildAdapter());
	}
	class ImgListener implements OnClickListener{
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent=new Intent();
			intent.setClass(growActivity.this, record_UI.class);
			startActivity(intent);
			//finish();
		}
	}
}

